package lgkj.meicqt20.ui;

import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.utils.tilepuzzle.BoardView;
import lgkj.meicqt20.utils.tilepuzzle.ClassBoardModel;
import lgkj.meicqt20.utils.tilepuzzle.ClassPaint;
import lgkj.meicqt20.utils.tilepuzzle.ClassSQLite;

/**
 * Created by Administrator on 2019/1/22 0022.
 */

public class PlayActivity extends BaseActivity {
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.iv_puzzle)
    ImageView ivPuzzle;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_rank)
    TextView tvRank;
    @BindView(R.id.ll)
    LinearLayout llCont;
    private ClassBoardModel classBoardModel;
    /**
     * 窗口的宽度
     */
    private int screenWidth = 0;
    /**
     * 窗口的高度
     */
    private int screenHeight = 0;


    private BoardView boardView;
    private ClassSQLite classSQLite;
    private int gone = 0;
    private ClassPaint classPaint;

    static float strHeight;


    static int menuIdx;

    @Override
    public int setContentView() {
        return R.layout.activity_play;
    }

    @Override
    public void init() {
        displayDifferen();
        classSQLite = new ClassSQLite(PlayActivity.this);
        Map<String, String> params = new HashMap<>();
        HttpUtils.getHttp(params, AllKindsOfConstant.toPuzzleUrl, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {

            }

            @Override
            public void errorHttp(String msg) {

            }
        });
        HttpUtils.getQRCode(PlayActivity.this, ivCode);
        initview();
        this.screenHeight = 1260;
        this.screenWidth = 840;

        Log.d("GPA", "获取GamePlayActivity屏幕属性成功" + this.screenHeight + "==" + this.screenWidth);

        this.classBoardModel = (ClassBoardModel) PlayActivity.this.getApplication();
        classBoardModel.isResultSucess = false;
        this.classBoardModel.setScreenWidth(this.screenWidth);
        this.classBoardModel.setScreenHeight(this.screenHeight);

        this.classBoardModel.initData();

//        init();

        boardView = new BoardView(PlayActivity.this, this.classBoardModel);
//        classBoardModel.addString();
        classBoardModel.rearrangeFunnily(true);
        classBoardModel.randomize(true);
        llCont.addView(boardView);

        ivPuzzle.setImageBitmap(this.classBoardModel.orgImage);
        time();
        tvTips.setText("步数:" + gone + "步");
        tvTime.setText("计时:" + times + "秒");
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);

    }

    public void initview() {
        this.classPaint = new ClassPaint();
        this.classPaint.initPaint();
        Paint.FontMetrics fontMetrics = classPaint.paintBigRed.getFontMetrics();

        // 计算文字高度
        strHeight = fontMetrics.bottom - fontMetrics.top;
        menuIdx = 0;
        this.classBoardModel = (ClassBoardModel) PlayActivity.this.getApplication();
        this.classBoardModel.setScreenWidth(this.screenWidth);
        this.classBoardModel.setScreenHeight(this.screenHeight);

        System.out.println("MainUIController：初始化菜单 -> MainMenuUI" + strHeight);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            String info = messageEvent.getMessage();
            if ("back".equals(info)) {
                finish();
            } else {
                gone++;
                boardView.move(info);
                if (!classBoardModel.isResultSucess) {
                    tvTips.setText("步数:" + gone + "步");
                } else {
                    tvRank.setText("恭喜您本次拼图\n获得" + (1236 + gone) + "名");
                    tvRank.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void initData() {

    }

    private long times = 0;

    private void time() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                Message ms = new Message();
                ms.what = 1;
                mHandler.sendMessage(ms);
                times++;

            }
        };
        timer.schedule(task, 1000L, 1000L);
    }

    public Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            if (msg.what == 1 && !classBoardModel.isResultSucess) {
                tvTime.setText("计时:" + times + "秒");
            }
        }

    };
}
